{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Demo for the DoWhy causal API\n",
    "We show a simple example of adding a causal extension to any dataframe. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import os, sys\n",
    "sys.path.append(os.path.abspath(\"../../../\"))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "import dowhy.datasets\n",
    "import dowhy.api\n",
    "\n",
    "import numpy as np\n",
    "import pandas as pd\n",
    "\n",
    "from statsmodels.api import OLS"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>W0</th>\n",
       "      <th>v0</th>\n",
       "      <th>y</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>-1.289334</td>\n",
       "      <td>False</td>\n",
       "      <td>-3.324194</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>-1.100803</td>\n",
       "      <td>False</td>\n",
       "      <td>-2.035903</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>-3.564829</td>\n",
       "      <td>False</td>\n",
       "      <td>-5.986921</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>-2.036167</td>\n",
       "      <td>False</td>\n",
       "      <td>-2.869478</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>-2.781469</td>\n",
       "      <td>False</td>\n",
       "      <td>-6.127631</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>995</th>\n",
       "      <td>-1.178597</td>\n",
       "      <td>True</td>\n",
       "      <td>2.965997</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>996</th>\n",
       "      <td>0.223498</td>\n",
       "      <td>True</td>\n",
       "      <td>4.161892</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>997</th>\n",
       "      <td>-2.582064</td>\n",
       "      <td>False</td>\n",
       "      <td>-3.472824</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>998</th>\n",
       "      <td>-1.998788</td>\n",
       "      <td>False</td>\n",
       "      <td>-0.725005</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>999</th>\n",
       "      <td>-0.789288</td>\n",
       "      <td>False</td>\n",
       "      <td>-2.080137</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>1000 rows × 3 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "           W0     v0         y\n",
       "0   -1.289334  False -3.324194\n",
       "1   -1.100803  False -2.035903\n",
       "2   -3.564829  False -5.986921\n",
       "3   -2.036167  False -2.869478\n",
       "4   -2.781469  False -6.127631\n",
       "..        ...    ...       ...\n",
       "995 -1.178597   True  2.965997\n",
       "996  0.223498   True  4.161892\n",
       "997 -2.582064  False -3.472824\n",
       "998 -1.998788  False -0.725005\n",
       "999 -0.789288  False -2.080137\n",
       "\n",
       "[1000 rows x 3 columns]"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data = dowhy.datasets.linear_dataset(beta=5,\n",
    "        num_common_causes=1,\n",
    "        num_instruments = 0,\n",
    "        num_samples=1000,\n",
    "        treatment_is_binary=True)\n",
    "df = data['df']\n",
    "df['y'] = df['y'] + np.random.normal(size=len(df)) # Adding noise to data. Without noise, the variance in Y|X, Z is zero, and mcmc fails.\n",
    "#data['dot_graph'] = 'digraph { v ->y;X0-> v;X0-> y;}'\n",
    "\n",
    "treatment= data[\"treatment_name\"][0]\n",
    "outcome = data[\"outcome_name\"][0]\n",
    "common_cause = data[\"common_causes_names\"][0]\n",
    "df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "WARNING:dowhy.causal_model:Causal Graph not provided. DoWhy will construct a graph based on data inputs.\n",
      "INFO:dowhy.causal_graph:If this is observed data (not from a randomized experiment), there might always be missing confounders. Adding a node named \"Unobserved Confounders\" to reflect this.\n",
      "INFO:dowhy.causal_model:Model to find the causal effect of treatment ['v0'] on outcome ['y']\n",
      "WARNING:dowhy.causal_identifier:If this is observed data (not from a randomized experiment), there might always be missing confounders. Causal effect cannot be identified perfectly.\n",
      "INFO:dowhy.causal_identifier:Continuing by ignoring these unobserved confounders because proceed_when_unidentifiable flag is True.\n",
      "INFO:dowhy.causal_identifier:Instrumental variables for treatment and outcome:[]\n",
      "INFO:dowhy.causal_identifier:Frontdoor variables for treatment and outcome:[]\n",
      "INFO:dowhy.do_sampler:Using WeightingSampler for do sampling.\n",
      "INFO:dowhy.do_sampler:Caution: do samplers assume iid data.\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "<AxesSubplot:xlabel='v0'>"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAXIAAAEWCAYAAAB7QRxFAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjMuMiwgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy8vihELAAAACXBIW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PJUgDyzNyDa3MvC8iLoyIVwNjwFN03r9m5OzfKQ0Wz8g17A4C7wHeR+cM/VHg4kWPj3fvkwaWT3ZqqEXE64FPAFuBtwPP0nmC89Lulxyl82Sn7+yngeXWioZaZj4QEZuBRzPzMYCI+ChwuPslHzHiGnSekUtSce6RS1JxhlySijPkklScIZek4gy5JBVnyCWpOEMuScUZckkq7v8BwujdRYoW77kAAAAASUVORK5CYII=\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "# data['df'] is just a regular pandas.DataFrame\n",
    "df.causal.do(x=treatment,\n",
    "                     variable_types={treatment: 'b', outcome: 'c', common_cause: 'c'},\n",
    "                     outcome=outcome,\n",
    "                     common_causes=[common_cause],\n",
    "                     proceed_when_unidentifiable=True).groupby(treatment).mean().plot(y=outcome, kind='bar')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "WARNING:dowhy.causal_model:Causal Graph not provided. DoWhy will construct a graph based on data inputs.\n",
      "INFO:dowhy.causal_graph:If this is observed data (not from a randomized experiment), there might always be missing confounders. Adding a node named \"Unobserved Confounders\" to reflect this.\n",
      "INFO:dowhy.causal_model:Model to find the causal effect of treatment ['v0'] on outcome ['y']\n",
      "WARNING:dowhy.causal_identifier:If this is observed data (not from a randomized experiment), there might always be missing confounders. Causal effect cannot be identified perfectly.\n",
      "INFO:dowhy.causal_identifier:Continuing by ignoring these unobserved confounders because proceed_when_unidentifiable flag is True.\n",
      "INFO:dowhy.causal_identifier:Instrumental variables for treatment and outcome:[]\n",
      "INFO:dowhy.causal_identifier:Frontdoor variables for treatment and outcome:[]\n",
      "INFO:dowhy.do_sampler:Using WeightingSampler for do sampling.\n",
      "INFO:dowhy.do_sampler:Caution: do samplers assume iid data.\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "<AxesSubplot:xlabel='v0'>"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAXQAAAERCAYAAABrWly6AAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjMuMiwgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy8vihELAAAACXBIWXMAAAsTAAALEwEAmpwYAAAO0klEQVR4nO3db4xldX3H8ffH3YFt4kbq7BDp7uKQsEkLbS04RY0PihIjWiMhpRUTFAWyidVUU/vAP8na0LTRJ9Y/EMm2EFhjhBao2VbUkmqiJIXu7IL8WUK6IbY7W9RxQZDqKut++2Du6nSc2Xtn5uzcnd+8X8lk77nnN+d+H2zeOXvuuXdTVUiSVr8XDXsASVI3DLokNcKgS1IjDLokNcKgS1Ij1g/rhTdt2lTj4+PDenlJWpX27t37g6oam2/f0II+Pj7O5OTksF5eklalJP+10D4vuUhSIwy6JDXCoEtSI4Z2DV2ShuWFF15gamqKI0eODHuUBW3YsIEtW7YwMjIy8O8YdElrztTUFBs3bmR8fJwkwx7nV1QVhw8fZmpqinPOOWfg3/OSi6Q158iRI4yOjp6SMQdIwujo6KL/BWHQJa1Jp2rMj1vKfAZdkhrhNXRJa974h77c6fG+8/E/7PR4gzLojej6L6TUlWHFbS3ykoskrbAdO3bwqU996hfbH/3oR/n0pz+97OMadElaYddccw27du0C4NixY9x+++1cddVVyz5u30suSTYA3wRO762/s6o+NmfN6cAu4JXAYeBtVfWdZU8nSQ0aHx9ndHSUBx98kO9973tccMEFjI6OLvu4g1xD/ynw+qp6PskIcF+Sr1TV/bPWXAs8U1XnJrkS+ATwtmVPJ0mNuu6667j11lv57ne/yzXXXNPJMftecqkZz/c2R3o/NWfZZcBtvcd3ApfkVL/JU5KG6PLLL+erX/0qe/bs4Y1vfGMnxxzoLpck64C9wLnAjVX1wJwlm4GDAFV1NMmzwCjwgznH2Q5sBzj77LOXN7kkdW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]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "df.causal.do(x={treatment: 1}, \n",
    "              variable_types={treatment:'b', outcome: 'c', common_cause: 'c'}, \n",
    "              outcome=outcome,\n",
    "              method='weighting', \n",
    "              common_causes=[common_cause],\n",
    "              proceed_when_unidentifiable=True).groupby(treatment).mean().plot(y=outcome, kind='bar')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "scrolled": false
   },
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "INFO:dowhy.causal_model:Model to find the causal effect of treatment ['v0'] on outcome ['y']\n",
      "WARNING:dowhy.causal_identifier:If this is observed data (not from a randomized experiment), there might always be missing confounders. Causal effect cannot be identified perfectly.\n",
      "INFO:dowhy.causal_identifier:Continuing by ignoring these unobserved confounders because proceed_when_unidentifiable flag is True.\n",
      "INFO:dowhy.causal_identifier:Instrumental variables for treatment and outcome:[]\n",
      "INFO:dowhy.causal_identifier:Frontdoor variables for treatment and outcome:[]\n",
      "INFO:dowhy.do_sampler:Using WeightingSampler for do sampling.\n",
      "INFO:dowhy.do_sampler:Caution: do samplers assume iid data.\n",
      "INFO:dowhy.causal_model:Model to find the causal effect of treatment ['v0'] on outcome ['y']\n",
      "WARNING:dowhy.causal_identifier:If this is observed data (not from a randomized experiment), there might always be missing confounders. Causal effect cannot be identified perfectly.\n",
      "INFO:dowhy.causal_identifier:Continuing by ignoring these unobserved confounders because proceed_when_unidentifiable flag is True.\n",
      "INFO:dowhy.causal_identifier:Instrumental variables for treatment and outcome:[]\n",
      "INFO:dowhy.causal_identifier:Frontdoor variables for treatment and outcome:[]\n",
      "INFO:dowhy.do_sampler:Using WeightingSampler for do sampling.\n",
      "INFO:dowhy.do_sampler:Caution: do samplers assume iid data.\n"
     ]
    }
   ],
   "source": [
    "cdf_1 = df.causal.do(x={treatment: 1}, \n",
    "              variable_types={treatment: 'b', outcome: 'c', common_cause: 'c'}, \n",
    "              outcome=outcome, \n",
    "              dot_graph=data['dot_graph'],\n",
    "              proceed_when_unidentifiable=True)\n",
    "\n",
    "cdf_0 = df.causal.do(x={treatment: 0}, \n",
    "              variable_types={treatment: 'b', outcome: 'c', common_cause: 'c'}, \n",
    "              outcome=outcome, \n",
    "              dot_graph=data['dot_graph'],\n",
    "              proceed_when_unidentifiable=True)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>W0</th>\n",
       "      <th>v0</th>\n",
       "      <th>y</th>\n",
       "      <th>propensity_score</th>\n",
       "      <th>weight</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>-0.782669</td>\n",
       "      <td>False</td>\n",
       "      <td>-0.792179</td>\n",
       "      <td>0.825287</td>\n",
       "      <td>1.211700</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>-1.386957</td>\n",
       "      <td>False</td>\n",
       "      <td>-2.541074</td>\n",
       "      <td>0.943911</td>\n",
       "      <td>1.059422</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>-1.704419</td>\n",
       "      <td>False</td>\n",
       "      <td>-3.922381</td>\n",
       "      <td>0.970418</td>\n",
       "      <td>1.030484</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>-0.814005</td>\n",
       "      <td>False</td>\n",
       "      <td>-0.313702</td>\n",
       "      <td>0.834584</td>\n",
       "      <td>1.198202</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>0.029784</td>\n",
       "      <td>False</td>\n",
       "      <td>-0.307668</td>\n",
       "      <td>0.461182</td>\n",
       "      <td>2.168343</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>995</th>\n",
       "      <td>-0.587235</td>\n",
       "      <td>False</td>\n",
       "      <td>-0.086199</td>\n",
       "      <td>0.757990</td>\n",
       "      <td>1.319279</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>996</th>\n",
       "      <td>-0.518178</td>\n",
       "      <td>False</td>\n",
       "      <td>-0.673428</td>\n",
       "      <td>0.730369</td>\n",
       "      <td>1.369171</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>997</th>\n",
       "      <td>-0.285695</td>\n",
       "      <td>False</td>\n",
       "      <td>-2.356726</td>\n",
       "      <td>0.624268</td>\n",
       "      <td>1.601876</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>998</th>\n",
       "      <td>-1.146594</td>\n",
       "      <td>False</td>\n",
       "      <td>-4.006878</td>\n",
       "      <td>0.910335</td>\n",
       "      <td>1.098497</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>999</th>\n",
       "      <td>-1.636781</td>\n",
       "      <td>False</td>\n",
       "      <td>-4.698411</td>\n",
       "      <td>0.966051</td>\n",
       "      <td>1.035142</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>1000 rows × 5 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "           W0     v0         y  propensity_score    weight\n",
       "0   -0.782669  False -0.792179          0.825287  1.211700\n",
       "1   -1.386957  False -2.541074          0.943911  1.059422\n",
       "2   -1.704419  False -3.922381          0.970418  1.030484\n",
       "3   -0.814005  False -0.313702          0.834584  1.198202\n",
       "4    0.029784  False -0.307668          0.461182  2.168343\n",
       "..        ...    ...       ...               ...       ...\n",
       "995 -0.587235  False -0.086199          0.757990  1.319279\n",
       "996 -0.518178  False -0.673428          0.730369  1.369171\n",
       "997 -0.285695  False -2.356726          0.624268  1.601876\n",
       "998 -1.146594  False -4.006878          0.910335  1.098497\n",
       "999 -1.636781  False -4.698411          0.966051  1.035142\n",
       "\n",
       "[1000 rows x 5 columns]"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "cdf_0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "scrolled": false
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>W0</th>\n",
       "      <th>v0</th>\n",
       "      <th>y</th>\n",
       "      <th>propensity_score</th>\n",
       "      <th>weight</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>-2.094548</td>\n",
       "      <td>True</td>\n",
       "      <td>1.099802</td>\n",
       "      <td>0.013245</td>\n",
       "      <td>75.499593</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>-1.584775</td>\n",
       "      <td>True</td>\n",
       "      <td>1.996253</td>\n",
       "      <td>0.037724</td>\n",
       "      <td>26.508238</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>-2.287651</td>\n",
       "      <td>True</td>\n",
       "      <td>-0.454571</td>\n",
       "      <td>0.008865</td>\n",
       "      <td>112.808679</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>-2.287651</td>\n",
       "      <td>True</td>\n",
       "      <td>-0.454571</td>\n",
       "      <td>0.008865</td>\n",
       "      <td>112.808679</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>-0.427584</td>\n",
       "      <td>True</td>\n",
       "      <td>2.698249</td>\n",
       "      <td>0.308738</td>\n",
       "      <td>3.238989</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>995</th>\n",
       "      <td>1.803262</td>\n",
       "      <td>True</td>\n",
       "      <td>8.249998</td>\n",
       "      <td>0.979852</td>\n",
       "      <td>1.020562</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>996</th>\n",
       "      <td>-0.335329</td>\n",
       "      <td>True</td>\n",
       "      <td>4.826016</td>\n",
       "      <td>0.351590</td>\n",
       "      <td>2.844225</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>997</th>\n",
       "      <td>0.656759</td>\n",
       "      <td>True</td>\n",
       "      <td>8.514054</td>\n",
       "      <td>0.813633</td>\n",
       "      <td>1.229055</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>998</th>\n",
       "      <td>0.946281</td>\n",
       "      <td>True</td>\n",
       "      <td>5.565004</td>\n",
       "      <td>0.889192</td>\n",
       "      <td>1.124617</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>999</th>\n",
       "      <td>0.192547</td>\n",
       "      <td>True</td>\n",
       "      <td>3.447829</td>\n",
       "      <td>0.621941</td>\n",
       "      <td>1.607870</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>1000 rows × 5 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "           W0    v0         y  propensity_score      weight\n",
       "0   -2.094548  True  1.099802          0.013245   75.499593\n",
       "1   -1.584775  True  1.996253          0.037724   26.508238\n",
       "2   -2.287651  True -0.454571          0.008865  112.808679\n",
       "3   -2.287651  True -0.454571          0.008865  112.808679\n",
       "4   -0.427584  True  2.698249          0.308738    3.238989\n",
       "..        ...   ...       ...               ...         ...\n",
       "995  1.803262  True  8.249998          0.979852    1.020562\n",
       "996 -0.335329  True  4.826016          0.351590    2.844225\n",
       "997  0.656759  True  8.514054          0.813633    1.229055\n",
       "998  0.946281  True  5.565004          0.889192    1.124617\n",
       "999  0.192547  True  3.447829          0.621941    1.607870\n",
       "\n",
       "[1000 rows x 5 columns]"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "cdf_1"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Comparing the estimate to Linear Regression\n",
    "First, estimating the effect using the causal data frame, and the 95% confidence interval."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAANIAAAASCAYAAAAucYD2AAAHjUlEQVR4nO3be/BVVRUH8A9EoWlmYwXTTA9+pEVqmRZJCtFDGqUMLHuNpk1CTTqU5aOY0J81jVhJZM0UvcxHo1OkjSZZiIzI2MiU/DKyEgJBMCkhCBNM0f5Y+8w9nN8593Hu5b/7nbmz791nr/1Yj73XWvvcEYODg/roo4/uMLLF8zPwbPqc02Hf0/FbbMZurMfPMakF3TtxMx7Fk3gEv8EpJW1HYBbuxeP4L36PTxm+trNza6n67C0Z4wosw8NpHduxGpfisIo1dErzAXwbd+M/aS7XV/RdRCf8KqKOfJvRHJbq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hr0BwjlDCPAY2EzEbDDakOv/Y3sju954gYqQx32deQluM1glcniHhohzCS69Kn+MZHHR7XRVv6MqL/N4o++ugerf5G0UcffbSBviH10UcP8H/9I7v0iWMPrQAAAABJRU5ErkJggg==\n",
      "text/latex": [
       "$\\displaystyle 4.869733164425347$"
      ],
      "text/plain": [
       "4.869733164425347"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "(cdf_1['y'] - cdf_0['y']).mean()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAOwAAAASCAYAAABYSzJ9AAAIPklEQVR4nO2bebCXUxjHPzeRvYylxjB0L5F9jVAKXSpSdkaWoRiZbGWJdGWMbKms2YlhSNayVqOyZNC1015uiOTmciuV/PE97/zO7/ze/X0tzfy+M3fOr/M+z3ue87znOc9znudUUVNTQxlllLFuoIlP33bAI8D3wEpgPjAC2CLhu08C7gKmAr8Ba4EnI3i6A28CdcByYC7wHNA+Zx4bZxrZ1gLn5zCPc6z3Bf2tyUGupDxp5ZofQv9jhDxHAi8YupVoTb0BdPOhvQWYCHyHvuNSYAYwBNgyB/otkU5eAGYbnmXANOA8/G2hAugDTAd+B/4APgIuDKA/h2zfPlJfTR2GKuA9YBvgJeAboB1wCXAMcCjwS8iANq4D9kYTrQN2jaC/BbjSvP9FYAmwE3A8cCJwFqWGkobHxvbA3UbGTXOaRy1wQ8CzDsARwGsR74gjV1KeLHItQ5u2i99D5LkVGIh09jL6NlsD+wOdgAkO/WXAJ8BbwE/AJsDBQA3Q1/z+LgP9ycB9wA/AZGAh0BI4AXgI6Gpo1lo8TwJnmPc/DTQCXcx7DkHry0Yt6XUcS1+uwd6LjLU/8ioehiMF3YR2lzi4zAw+GzgcKSkIrYABwGJgL6QgD52BScBQio0vDY+NCuBRZOzjzLuyzgP00WoDnr1v2gdC+OPKlZQni1z1yBDiog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qPdfcq7QAAAABJRU5ErkJggg==\n",
      "text/latex": [
       "$\\displaystyle 0.18841744756332976$"
      ],
      "text/plain": [
       "0.18841744756332976"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "1.96*(cdf_1['y'] - cdf_0['y']).std() / np.sqrt(len(df))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Comparing to the estimate from OLS."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {
    "scrolled": false
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<table class=\"simpletable\">\n",
       "<caption>OLS Regression Results</caption>\n",
       "<tr>\n",
       "  <th>Dep. Variable:</th>            <td>y</td>        <th>  R-squared (uncentered):</th>      <td>   0.926</td>\n",
       "</tr>\n",
       "<tr>\n",
       "  <th>Model:</th>                   <td>OLS</td>       <th>  Adj. R-squared (uncentered):</th> <td>   0.926</td>\n",
       "</tr>\n",
       "<tr>\n",
       "  <th>Method:</th>             <td>Least Squares</td>  <th>  F-statistic:       </th>          <td>   6276.</td>\n",
       "</tr>\n",
       "<tr>\n",
       "  <th>Date:</th>             <td>Sat, 05 Dec 2020</td> <th>  Prob (F-statistic):</th>           <td>  0.00</td> \n",
       "</tr>\n",
       "<tr>\n",
       "  <th>Time:</th>                 <td>18:02:18</td>     <th>  Log-Likelihood:    </th>          <td> -1436.6</td>\n",
       "</tr>\n",
       "<tr>\n",
       "  <th>No. Observations:</th>      <td>  1000</td>      <th>  AIC:               </th>          <td>   2877.</td>\n",
       "</tr>\n",
       "<tr>\n",
       "  <th>Df Residuals:</th>          <td>   998</td>      <th>  BIC:               </th>          <td>   2887.</td>\n",
       "</tr>\n",
       "<tr>\n",
       "  <th>Df Model:</th>              <td>     2</td>      <th>                     </th>              <td> </td>   \n",
       "</tr>\n",
       "<tr>\n",
       "  <th>Covariance Type:</th>      <td>nonrobust</td>    <th>                     </th>              <td> </td>   \n",
       "</tr>\n",
       "</table>\n",
       "<table class=\"simpletable\">\n",
       "<tr>\n",
       "   <td></td>     <th>coef</th>     <th>std err</th>      <th>t</th>      <th>P>|t|</th>  <th>[0.025</th>    <th>0.975]</th>  \n",
       "</tr>\n",
       "<tr>\n",
       "  <th>x1</th> <td>    1.8467</td> <td>    0.022</td> <td>   82.505</td> <td> 0.000</td> <td>    1.803</td> <td>    1.891</td>\n",
       "</tr>\n",
       "<tr>\n",
       "  <th>x2</th> <td>    5.0277</td> <td>    0.067</td> <td>   74.507</td> <td> 0.000</td> <td>    4.895</td> <td>    5.160</td>\n",
       "</tr>\n",
       "</table>\n",
       "<table class=\"simpletable\">\n",
       "<tr>\n",
       "  <th>Omnibus:</th>       <td> 0.268</td> <th>  Durbin-Watson:     </th> <td>   1.984</td>\n",
       "</tr>\n",
       "<tr>\n",
       "  <th>Prob(Omnibus):</th> <td> 0.874</td> <th>  Jarque-Bera (JB):  </th> <td>   0.291</td>\n",
       "</tr>\n",
       "<tr>\n",
       "  <th>Skew:</th>          <td> 0.040</td> <th>  Prob(JB):          </th> <td>   0.864</td>\n",
       "</tr>\n",
       "<tr>\n",
       "  <th>Kurtosis:</th>      <td> 2.973</td> <th>  Cond. No.          </th> <td>    3.02</td>\n",
       "</tr>\n",
       "</table><br/><br/>Notes:<br/>[1] R² is computed without centering (uncentered) since the model does not contain a constant.<br/>[2] Standard Errors assume that the covariance matrix of the errors is correctly specified."
      ],
      "text/plain": [
       "<class 'statsmodels.iolib.summary.Summary'>\n",
       "\"\"\"\n",
       "                                 OLS Regression Results                                \n",
       "=======================================================================================\n",
       "Dep. Variable:                      y   R-squared (uncentered):                   0.926\n",
       "Model:                            OLS   Adj. R-squared (uncentered):              0.926\n",
       "Method:                 Least Squares   F-statistic:                              6276.\n",
       "Date:                Sat, 05 Dec 2020   Prob (F-statistic):                        0.00\n",
       "Time:                        18:02:18   Log-Likelihood:                         -1436.6\n",
       "No. Observations:                1000   AIC:                                      2877.\n",
       "Df Residuals:                     998   BIC:                                      2887.\n",
       "Df Model:                           2                                                  \n",
       "Covariance Type:            nonrobust                                                  \n",
       "==============================================================================\n",
       "                 coef    std err          t      P>|t|      [0.025      0.975]\n",
       "------------------------------------------------------------------------------\n",
       "x1             1.8467      0.022     82.505      0.000       1.803       1.891\n",
       "x2             5.0277      0.067     74.507      0.000       4.895       5.160\n",
       "==============================================================================\n",
       "Omnibus:                        0.268   Durbin-Watson:                   1.984\n",
       "Prob(Omnibus):                  0.874   Jarque-Bera (JB):                0.291\n",
       "Skew:                           0.040   Prob(JB):                        0.864\n",
       "Kurtosis:                       2.973   Cond. No.                         3.02\n",
       "==============================================================================\n",
       "\n",
       "Notes:\n",
       "[1] R² is computed without centering (uncentered) since the model does not contain a constant.\n",
       "[2] Standard Errors assume that the covariance matrix of the errors is correctly specified.\n",
       "\"\"\""
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "model = OLS(np.asarray(df[outcome]), np.asarray(df[[common_cause, treatment]], dtype=np.float64))\n",
    "result = model.fit()\n",
    "result.summary()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.5"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": false,
   "sideBar": true,
   "skip_h1_title": true,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
